﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Experimental.UI;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;
using Microsoft.MixedReality.Toolkit.Utilities;
using Mirror;
using MrPP.SampleLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace MrPP.SampleGame { 
    public class TooltipManager : GDGeek.Singleton<TooltipManager>, IMixedRealityHandJointHandler,/* IMixedRealityGestureHandler, */IMixedRealityInputHandler
    {
        [Serializable]
        public class OnSelectGesComplete : UnityEvent<Vector3, Quaternion> { }
        public OnSelectGesComplete _callback;
        private void OnEnable()
        {
            CoreServices.InputSystem.RegisterHandler<IMixedRealityHandJointHandler>(this);
            CoreServices.InputSystem.RegisterHandler<IMixedRealityInputHandler>(this);
        }
      

        private void OnDisable()
        {
            CoreServices.InputSystem?.RegisterHandler<IMixedRealityHandJointHandler>(this);
            CoreServices.InputSystem?.RegisterHandler<IMixedRealityInputHandler>(this);
        }
        private float _time = 0.0f;

        [SerializeField]
        private Handedness myHandedness;

        private Vector3 _palmPos;
        private Quaternion _palmRot;

        private static bool clicked = false;
        private static float clickFrezTime = 2.0f;

        public void OnHandJointsUpdated(InputEventData<IDictionary<TrackedHandJoint, MixedRealityPose>> eventData)
        {

            if ((eventData.Handedness & myHandedness) != 0)
            {
                if (eventData.InputData.TryGetValue(TrackedHandJoint.Palm, out MixedRealityPose pose))
                {
                    _palmPos = pose.Position;
                    _palmRot = pose.Rotation;
                }

            }
        }
        public void OnInputUp(InputEventData eventData)
        {

           Debug.LogError("OnInputUp");
        }
        public void Update() {
            _time += Time.deltaTime;
           
        }
        public void OnInputDown(InputEventData eventData)
         {

            if (_time > 0.15f) { 
           
                Debug.LogError("OnInputDown" + eventData.InputSource.SourceType.ToString());
                if ((eventData.Handedness & myHandedness) != 0)
                {
                    _callback?.Invoke(_palmPos, _palmRot);
                    _time = 0.0f;
                }
            }
        }
    }
}